﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace 软构大作业
{

    public partial class Form11 : Form
    {
        private new AutoAdaptWindowsSize AutoSize;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }

        private string bookId; // 保存当前修改的图书ID
        public Form11()
        {
            InitializeComponent();
            // 设置窗体的KeyPreview属性为true，以便窗体可以接收键盘事件
            this.KeyPreview = true;
            // 为窗体添加KeyDown事件处理
            this.KeyDown += Form11_KeyDown;
        }

        // 设置图书信息方法
        public void SetBookInfo(string id, string title, string author, string publisher, string location, int stock, string category)
        {
            bookId = id;
            textBox1.Text = title;
            textBox2.Text = author;
            textBox3.Text = publisher;
            textBox4.Text = location;
            numericUpDown1.Value = stock;
            textBox5.Text = category;
        }

        private void Form11_FormClosed(object sender, FormClosedEventArgs e)
        {
            //重置界面并返回
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox4.Text = "";
            numericUpDown1.Value = 0;
            Global.Instance.form11login=null;
            Global.Instance.form9login.Show();
        }
        private void Form11_KeyDown(object sender, KeyEventArgs e)
        {
            // 检查是否按下Enter键
            if (e.KeyCode == Keys.Enter)
            {
                // 触发button1的点击事件
                button1.PerformClick();
                // 标记事件已处理，防止其他控件处理该事件
                e.Handled = true;
                e.SuppressKeyPress = true; // 防止发出"叮"的声音
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 获取用户输入的图书信息
                string title = textBox1.Text.Trim();
                string author = textBox2.Text.Trim();
                string publisher = textBox3.Text.Trim();
                string location = textBox4.Text.Trim();
                int stock = (int)numericUpDown1.Value;
                string category = textBox5.Text.Trim();

                // 验证用户输入
                if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(author) || string.IsNullOrEmpty(publisher) ||
                    string.IsNullOrEmpty(location) || string.IsNullOrEmpty(category))
                {
                    MessageBox.Show("所有字段均为必填项！", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // 使用Data类向数据库更新数据
                Data dao = new Data();
                string updateSql = @"UPDATE library_db.books 
                                    SET title = @Title, author = @Author, publisher = @Publisher, 
                                        location = @Location, stock = @Stock, category = @Category 
                                    WHERE book_id = @BookId";

                SqlParameter[] parameters = new SqlParameter[]
                {
                    new SqlParameter("@Title", title),
                    new SqlParameter("@Author", author),
                    new SqlParameter("@Publisher", publisher),
                    new SqlParameter("@Location", location),
                    new SqlParameter("@Stock", stock),
                    new SqlParameter("@Category", category),
                    new SqlParameter("@BookId", bookId)
                };

                int rowsAffected = dao.ExecuteSql(updateSql, parameters);

                if (rowsAffected > 0)
                {
                    MessageBox.Show("图书修改成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    // 修改成功后返回图书管理界面并刷新图书列表
                    this.Close();
                    Global.Instance.form9login.RefreshBookList(); // 调用Form9的刷新方法
                }
                else
                {
                    MessageBox.Show("修改图书失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                dao.DaoClose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改图书过程中发生错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox4.Text = "";
            numericUpDown1.Value = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox4.Text = "";
            numericUpDown1.Value = 0;
            Global.Instance.form11login.Hide();
            Global.Instance.form9login.Show();
        }

        private void Form11_Load(object sender, EventArgs e)
        {
            AutoSize = new AutoAdaptWindowsSize(this);
            this.SizeChanged += new EventHandler(Form_SizeChanged);
        }

        private void Form_SizeChanged(object sender, EventArgs e)
        {//窗体大小改变事件
            if (AutoSize != null) // 一定加这个判断，电脑缩放布局不是100%的时候，会报错
            {
                AutoSize.FormSizeChanged();
            }
        }
    }
}
